package com.ptszyxx.popose.module.main.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProviders;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.HomeTabEnum;
import com.ptszyxx.popose.common.utils.YBusUtil;
import com.ptszyxx.popose.databinding.FragmentHomeBinding;
import com.ptszyxx.popose.module.main.home.vm.HomeVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.user.UserResult;
import com.ysg.utils.YDialogUtil;
import com.ysg.utils.YToastUtil;
import com.ysg.widget.dialog.AccostDialog;
import com.ysg.widget.tab.YTabLayout;
import java.util.List;

public class HomeFragment extends BaseFragment<FragmentHomeBinding, HomeVM> {
    private HomeTabEnum tabEnum;

    public static HomeFragment getInstance() {
        return new HomeFragment();
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public HomeVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(HomeVM.class);
    }

    @Override
    public void initView() {
        super.initView();
        initTab();
//        if (YChannelUtil.getInstance().beta()){
//            binding.rbOnline.setVisibility(View.VISIBLE);
//            binding.rbAccount.setVisibility(View.VISIBLE);
//            binding.rbOther.setVisibility(View.VISIBLE);
//            binding.viewTest.setVisibility(View.VISIBLE);
//        }
    }

    @Override
    public void initData() {
        super.initData();
//        viewModel.requestRandomUserList();
    }

    @Override
    public void initViewObservable() {
        viewModel.uc.onRandomUserEvent.observe(this, list -> showDialogRandom((List<UserResult>)list));
        viewModel.uc.onRefreshEvent.observe(this, o -> onRefreshFinish((boolean) o));
    }

    public void initAdapter() {
        binding.refreshLayout.setOnRefreshListener(refreshLayout -> {
            YBusUtil.refreshHomeTab(true, tabEnum);
        });
        binding.refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            YBusUtil.refreshHomeTab(false, tabEnum);
        });
    }

    /**
     * 随机用户-今日缘分
     */
    private void showDialogRandom(List<UserResult> list) {
        AccostDialog dialog = YDialogUtil.getInstance().showRandomUser(getContext(), list);
        dialog.setOnConfirmListener(new AccostDialog.OnConfirmListener() {
            @Override
            public void onConfirmClick(String ids, double sum) {
                if (viewModel.walletEntity != null){
                    try {
                        if (Double.valueOf(viewModel.walletEntity.getZuanshi()) >= sum){
                            viewModel.requestSelectAccost(ids);
                            dialog.dismiss();
                        }else{
                            YToastUtil.showShort(R.string.tip_no_diamond);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void onRefreshFinish(boolean noMore) {
        binding.refreshLayout.finishRefresh();
        binding.refreshLayout.finishLoadMore();
        binding.refreshLayout.setNoMoreData(noMore);
    }

    private void setNoMoreData(HomeTabEnum tabEnum) {
        switch (tabEnum){
            case city:
                binding.refreshLayout.setNoMoreData(viewModel.noMoreCity);
                break;
            case near:
                binding.refreshLayout.setNoMoreData(viewModel.noMoreNear);
                break;
            case online:
                binding.refreshLayout.setNoMoreData(viewModel.noMoreOnline);
                break;
            case account:
                binding.refreshLayout.setNoMoreData(viewModel.noMoreAccount);
                break;
            case other:
                binding.refreshLayout.setNoMoreData(false);
                break;
        }
    }

    private void initTab() {
        String[] mTitles = {"同城", "活跃"};
        binding.tabLayout.initGrid(mTitles);
        binding.tabLayout.setOnClickListener(new YTabLayout.OnClickListener() {
            @Override
            public void onSelectClick(int tab) {
                showFragment(HomeTabEnum.getTab(tab));
            }
        });
        showFragment(HomeTabEnum.city);
    }

    private HomeTabFragment cityFragment;
    private HomeWorldFragment worldFragment;
    private HomeWorldFragment worldNearFragment;
    private HomeTabFragment nearFragment;
    private HomeTabFragment onlineFragment;
    private HomeTabFragment accountFragment;
    private HomeTabOtherFragment otherFragment;
    public void showFragment(HomeTabEnum tabEnum){
        this.tabEnum = tabEnum;
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if (cityFragment != null) transaction.hide(cityFragment);
        if (worldFragment != null) transaction.hide(worldFragment);
        if (worldNearFragment != null) transaction.hide(worldNearFragment);
        if (nearFragment != null) transaction.hide(nearFragment);
        if (onlineFragment != null) transaction.hide(onlineFragment);
        if (accountFragment != null) transaction.hide(accountFragment);
        if (otherFragment != null) transaction.hide(otherFragment);

        switch (tabEnum){
            case city:
                if (worldFragment == null){
                    worldFragment = new HomeWorldFragment(tabEnum);
                    transaction.add(R.id.fragmentManager, worldFragment);
                }else{
                    transaction.show(worldFragment);
                }
                break;
            case near:
                if (worldNearFragment == null){
                    worldNearFragment = new HomeWorldFragment(tabEnum);
                    transaction.add(R.id.fragmentManager, worldNearFragment);
                }else{
                    transaction.show(worldNearFragment);
                }
                break;
            case online:
                if (onlineFragment == null){
                    onlineFragment = new HomeTabFragment(tabEnum);
                    transaction.add(R.id.fragmentManager, onlineFragment);
                }else{
                    transaction.show(onlineFragment);
                }
                break;
            case account:
                if (accountFragment == null){
                    accountFragment = new HomeTabFragment(tabEnum);
                    transaction.add(R.id.fragmentManager, accountFragment);
                }else{
                    transaction.show(accountFragment);
                }
                break;
            case other:
                if (otherFragment == null){
                    otherFragment = new HomeTabOtherFragment(tabEnum);
                    transaction.add(R.id.fragmentManager, otherFragment);
                }else{
                    transaction.show(otherFragment);
                }
                break;
        }
        transaction.commit();

        setNoMoreData(tabEnum);
    }


}
